﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Azure.AI.TextAnalytics;
using Newtonsoft.Json;

namespace TabbedTemplate.Services
{
    public class TextAnalysisService:ITextAnalysisService
    {
        public async Task<DocumentSentiment> AnalysisEmotionAsync(string content) {
            var client = new TextAnalyticsClient(Confiddential.AzureSettings.endpoint, Confiddential.AzureSettings.credentials);
            
            var text = ConvertHtmlToText(content);
            if (string.IsNullOrEmpty(text)) {
                return null;
            }
            var translated = await TranslateToENAsync(text);

            var resDocument =  client.AnalyzeSentiment(translated);
            return resDocument;
        }

        public string ConvertHtmlToText(string html) {
            string inputstring = html.ToString();
            string htmlTag = "<(.+?)>";
            inputstring = Regex.Replace(inputstring, htmlTag, "");
            return inputstring;
        }

        public async Task<string> TranslateToENAsync(string content) {
            // Input and output languages are defined as parameters.
            string route = "/translate?api-version=3.0&to=en&to=it";
            string textToTranslate = content;
            object[] body = new object[] { new { Text = textToTranslate } };
            var requestBody = JsonConvert.SerializeObject(body);

            using (var client = new HttpClient())
            using (var request = new HttpRequestMessage())
            {
                // Build the request.
                request.Method = HttpMethod.Post;
                request.RequestUri = new Uri(Confiddential.AzureSettings.Translateendpoint + route);
                request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                request.Headers.Add("Ocp-Apim-Subscription-Key", Confiddential.AzureSettings.TranslatesubscriptionKey);
                request.Headers.Add("Ocp-Apim-Subscription-Region", Confiddential.AzureSettings.TranslateLocation);

                // Send the request and get response.
                HttpResponseMessage response = await client.SendAsync(request);
                // Read response as a string.
                var res = await response.Content.ReadAsStringAsync();
                res =res.Substring(1, res.Length - 2);
                var Root = JsonConvert.DeserializeObject<Root>(res);
                return Root.translations[0].text;

            }
        }
        public class DetectedLanguage
        {
            public string language { get; set; }
            public double score { get; set; }
        }

        public class Translation
        {
            public string text { get; set; }
            public string to { get; set; }
        }

        public class Root
        {
            public DetectedLanguage detectedLanguage { get; set; }
            public List<Translation> translations { get; set; }
        }

    }
   
}
